package chatbot.service;

import chatbot.entity.Step;
import chatbot.enums.ExpressionConstant;
import chatbot.enums.StepNameConstant;
import chatbot.utils.CommonKit;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.util.*;

public class Parsescript {
    public static final HashMap<String, Step> stepHashMap;
    static {
        stepHashMap=parseScript("script1.json");
    }

    /**
     解析script脚本,返回Map结构 key 是每个步骤的名字,value是每个步骤分支和答案
     * @param scriptPath
     * @return
     */
    public static   HashMap<String, Step> parseScript(String scriptPath){
        ObjectMapper objectMapper = new ObjectMapper();
        String classPath = CommonKit.getClassPath();
        HashMap<String, Step> stepName2Step=new HashMap<>();
        try {
            Map<String,Object> map = objectMapper.readValue(new File(classPath + "/"+scriptPath), Map.class);
            Set<Map.Entry<String, Object>> entrySet = map.entrySet();
            Iterator<Map.Entry<String, Object>> entryIterator = entrySet.iterator();
            while(entryIterator.hasNext()){
                Step step=new Step();
                Map.Entry<String, Object> next = entryIterator.next();
                String stepName=next.getKey();
                step.setStepName(stepName);
                Map<String, Object> valueMap = (Map<String, Object>) next.getValue();
                Set<Map.Entry<String, Object>> entries = valueMap.entrySet();
                Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
                while(iterator.hasNext())
                {
                    Map.Entry<String, Object> next1 = iterator.next();
                    step.addAnswerStepNames(next1.getKey(),(String)next1.getValue());
                }

                stepName2Step.put(stepName,step);
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
       return  stepName2Step;
    }

    /**
     根据step名字和key的名字,获取每个问题的答案
     * @param stepName
     * @param anserkey
     * @return
     */
    public static String getStepValue(String stepName,String anserkey){
        String str=stepHashMap.get(stepName).getAnswerStepNames().get(anserkey);
        return str;
    }

    public static void  printStepValue(String stepName,String anserkey){
       String str=stepHashMap.get(stepName).getAnswerStepNames().get(anserkey);
       //return str;
       System.out.println(str);
    }

    /**
     * 根据用户的回答搜索每个答案的分支 branch
     * @param str
     * @param step
     * @return
     */
    public static Step searchStepBybranch(String str,Step step){
        Collection<String> values = step.getAnswerStepNames().values();
        Iterator<String> iterator = values.iterator();
        while(iterator.hasNext()){
            String next = iterator.next();
            if(next.contains(str)){
                return stepHashMap.get(next.split(",")[1]);
            }
        }
        return null;
    }

    /**
     获取剩下步骤的名字
     * @param step
     * @return
     */
    public static Object[] getRestStep(Step step){
        Object[] returnValue=new Object[2];

        String[] branches=ExpressionConstant.BRANCH.getName().split(",");
        boolean flag=false;
        for(int i=0;i<branches.length;i++){
            if(step.getAnswerStepNames().containsKey(branches[i])){
                flag=true;
                break;
            }
        }
        if(!flag){
            if(step.getAnswerStepNames().containsKey(ExpressionConstant.SPEAK.getName())){
                //printStepValue(step.getStepName(),ExpressionConstant.SPEAK.getName());
            }
            if(step.getAnswerStepNames().containsKey(ExpressionConstant.DEFAULTI.getName())){
                String msg=getStepValue(StepNameConstant.DEFAULTPRO.getName(),ExpressionConstant.SPEAK.getName());
                returnValue[1]=msg;
            }
            returnValue[0]=null;
        }else{
            returnValue[0]=step;
        }
        return returnValue;
    }
}
